﻿using System;
using Mono.Cecil;

namespace CILADA.Engine
{
    static class TypeUtilities
    {
        public static TypeClassification GetTypeClassification(TypeDefinition type)
        {
            if (null == type) throw new ArgumentException("Cannot find type classification of a null type definition!");

            if (type.IsInterface)
            {
                return TypeClassification.Interface;
            }
            else if (type.IsEnum)
            {
                return TypeClassification.Enum;
            }
            else if (type.IsValueType)
            {
                return TypeClassification.Struct;
            }

            return TypeClassification.Class;
        }

        public static TypeVisibility? GetVisibilityLevel(TypeDefinition type)
        {
            if (null == type) throw new ArgumentException("Cannot find the visibility of a null type definition!");

            if (type.IsPublic)
            {
                return TypeVisibility.Public;
            }
            else
            {
                return null; // TODO Fix this so it gets the correct type for non public types.
            }
        }

        public static string FormatItemIntoType(string item)
        {
            // remove ` used in generics
            int ixApos = item.LastIndexOf("`");
            if (ixApos > -1)
            {
                item = item.Substring(0, ixApos);
            }

            // remove :: used to indicate methods.
            int ixCC = item.LastIndexOf("::");
            return (ixCC >= 0) ? item.Substring(0, ixCC) : item;
        }
    }
}
